<?php
/*
*All rights reserved: Json.
*Links:http://www.liyaoquan.cn.
*Links:http://imarkchina.cn.
*Links:http://www.imarkchina.cn.
*/

/**
 * 博客后台分类模型，负责数据，增，删，改，查
 * 因为没有博客数据没使用数据库，所以没有继承 Model 类
 */
Class GetlistModel extends PublicModel{



	/**
	 * 博客分类操作
	 * @return [type] [description]
	 */
	Public function blogAllList(){
		$dir = loadfile(C('post.listid'));
		foreach ($dir as $k => $v) {
			$lfile = C('post.postid').$k.'.php';
			$include = loadfile($lfile);
			if ($include) {
				$dir[$k]['countpost'] = count($include);
			} else {
				$dir[$k]['countpost'] = 0;
			}
			$dir[$k]['dess'] = Getstr($v['des'], 20);
			$dir[$k]['keywordd'] = Getstr($v['keyword'], 20);
		}
		return $dir;
	}

	/**
	 * 更新博客分类排序
	 * @param  [type] $arr [接收数组]
	 * @return [type]      [description]
	 */
	Public function upDateList($arr){
		$dir = loadfile(C('post.listid'));
		foreach ($dir as $k => $v) {
			foreach ($arr as $z => $q) {
				$dir[$z]['ord'] = $q;
			}
		}
		uasort($dir, 'List_Sort');
		if (varfile(C('post.listid'), $dir)) {
			return true;
		}
	}

	/**
	 * 删除博客分类
	 * @param  [type] $arr [接收数组与字符串]
	 * @return [type]      [description]
	 */
	Public function delList($arr){
		$iddir = C('post.listid');
		$filedata = C('post.postid');
		$iddata = loadfile($iddir);
		if (is_array($arr)) {
			foreach ($iddata as $k => $v) {
				foreach ($arr as $z) {
					unset($iddata[$z]);
					if (file_exists($filedata.$z.'.php')) {
						unlink($filedata.$z.'.php');
					}
					if (file_exists(C('post.postrecovery').$z.'.php')) {
						unlink(C('post.postrecovery').$z.'.php');
					}
				}
			}
		} elseif (is_string($arr)) {
			unset($iddata[$arr]);
			if (file_exists($filedata.$arr.'.php')) {
				unlink($filedata.$arr.'.php');
			}
			if (file_exists(C('post.postrecovery').$arr.'.php')) {
				unlink(C('post.postrecovery').$arr.'.php');
			}
		}
		if (varfile($iddir, $iddata)) {
			return true;
		}
	}

	/**
	 * 取需要修改的分类数据
	 * @param  [type] $id [description]
	 * @return [type]     [description]
	 */
	Public function getList($id = null){
		if (is_null($id)) return;
		$dir = loadfile(C('post.listid'));
		return $dir[$id];
	}

	/**
	 * 写入分类数据
	 * @param  [type] $arr [接收数组]
	 * @return [type]      [description]
	 */
	Public function writeData($arr){
		if (empty($arr['date']) && empty($arr['time'])) {
			$arr['date'] = date('Y.m.d');
			$arr['time'] = date('H:i');
		}
		if (!empty($arr['path'])) {
			$path = $arr['path'];
		} else {
			$path = randCode(6);
			if (file_exists(C('post.postid').$path.'.php')) {
				$path = randCode(6);
			}
			$arr['path'] = $path;
		}
		$data[$path] = $arr;
		$dir = C('post.listid');
		$file = loadfile($dir);
		$lastarray = array_merge($file, $data);
		uasort($lastarray, 'List_Sort');
		if (varfile($dir, $lastarray)) {
			if (!file_exists(C('post.postid').$path.'.php')) {
				varfile(C('post.postid').$path.'.php', array());
			}
			return true;
		}
	}
}